import { _decorator, Component, Layers, Node, Prefab, random, randomRangeInt, resources, Sprite, SpriteFrame, toDegree, UITransform } from 'cc';
import { Joystick } from './Joystick';
import { Player } from './Player';
import { Const } from './Const';
import { Util } from './Util';
import { Enemy } from './Enemy';
import { Constant } from './Constant';
import { ResMgr } from './ResMgr';
import { Global } from './Global';
import { SkillBottom } from './SkillBottom';
const { ccclass, property } = _decorator;

@ccclass('GameWnd')
export class GameWnd extends Component {
    @property(Node)
    ndPlayer: Node;
    @property(Node)
    ndJoystick: Node;
    @property(Node)
    ndSkillBtn: Node;
    @property(Node)
    ndAttackBtn: Node;
    @property(Node)
    ndIndicator: Node;
    @property(Node)
    ndGround: Node;
    @property(Node)
    ndTalentPanel: Node;

    protected onLoad(): void {
        Const.ndPlayer = this.ndPlayer;
        Const.player = this.ndPlayer.getComponent(Player);

        const createSubNode = (name: string) => {
            const subNode = new Node(name);
            this.node.addChild(subNode);
            subNode.layer = Layers.Enum.UI_2D;
            return subNode;
        }

        Const.ndEnemyParent = createSubNode('ndEnemyParent');
        Const.ndDamageTextParent = createSubNode('ndTextParent');
        Const.ndWeapon = createSubNode('Weapon');

    }

    protected onEnable(): void {
        const playerScript = this.ndPlayer.getComponent(Player);
        this.ndJoystick.getComponent(Joystick).onTouchEvent((event: number, radian: number | null) => {
            switch (event) {
                case Joystick.Event.START:
                    playerScript.isMoving = true;
                    break;
                case Joystick.Event.MOVE:
                    if (radian !== null)
                        playerScript.moveDirection = radian;
                    break;
                case Joystick.Event.END:
                case Joystick.Event.CANCEL:
                    playerScript.isMoving = false;
                    break;

                default:
                    break;
            }
        });

        this.ndSkillBtn.getComponent(SkillBottom).onTouchEvent((event: number, radian: number) => {
            switch (event) {
                case SkillBottom.Event.START:
                    this.ndIndicator.active = true;
                    break;
                case SkillBottom.Event.MOVE:
                    this.ndIndicator.angle = toDegree(radian);
                    break;
                case SkillBottom.Event.END:
                    this.ndIndicator.active = false;
                    Const.player.castFireBall(radian);
                    break;
                case SkillBottom.Event.CANCEL:
                    this.ndIndicator.active = false;
                    break;
                default:
                    break;
            }
        });
        this.ndAttackBtn.getComponent(SkillBottom).coldDownTime = 0.1;
        this.ndAttackBtn.getComponent(SkillBottom).onTouchEvent((event: number, radian: number) => {
            switch (event) {
                case SkillBottom.Event.START:
                    Const.player.startShootBullet();
                    break;
                case SkillBottom.Event.MOVE:
                    Const.player.isAiming = false;
                    Const.player.setGunAngel(toDegree(radian));
                    break;
                case SkillBottom.Event.END:
                    Const.player.isAiming = true;
                    break;
                case SkillBottom.Event.CANCEL:
                    Const.player.isAiming = true;
                    break;
                default:
                    break;
            }
        })

        playerScript.isMoving = true;
        this.ndIndicator.active = false;
    }
    start() {
        // this.createEnemys();
        this.generateGround()
        // Const.ndPlayer.getComponent(Player).startEndlessArrow();
        // Const.ndPlayer.getComponent(Player).startSurrondingSwords();
        // Const.ndPlayer.getComponent(Player).startFireBall();
        // Const.ndPlayer.getComponent(Player).startLightning();
        this.schedule(() => {
            this.onGameSpawned()
        }, 2);
    }
    update(deltaTime: number) {
        // this.ndCamera.worldPosition = this.ndPlayer.worldPosition;
        // this.updateEnemyLayers();
    }

    private onGameSpawned() {
        const minRange = -1024;
        const maxRange = 1024;
        if (Const.ndEnemyParent.children.length < 50) {
            for (let i = 0; i < 5; i++) {
                const x = randomRangeInt(minRange, maxRange);
                const y = randomRangeInt(minRange, maxRange);
                const enemy = Global.getNode(Constant.PrefabUrl.ENEMY, Const.ndEnemyParent);

                enemy.setPosition(x, y);
                const enemyScript = enemy.getComponent(Enemy);
                enemyScript.speed = 1.5;
                enemyScript.hp = 100;
            }
        }

    }


    private updateEnemyLayers() {
        const enemys = Const.ndEnemyParent.children;
        for (let i = 0; i < enemys.length; i++) {
            for (let j = i + 1; j < enemys.length; j++) {
                if (enemys[j].position.y > enemys[i].position.y) {
                    const index = enemys[i].getSiblingIndex();
                    enemys[j].setSiblingIndex(index);
                }
            }
        }
    }

    private createEnemys() {
        for (let i = 0; i < 10; i++) {
            const enemy = Global.getNode(Constant.PrefabUrl.ENEMY, Const.ndEnemyParent);
            enemy.setPosition(randomRangeInt(-1000, 1000), randomRangeInt(-1000, 1000));
            enemy.getComponent(Enemy).speed = 0.8;
        }
    }

    private generateGround() {
        resources.loadDir('grounds/', SpriteFrame, (err, res) => {
            if (err) {
                return;
            }
            for (let i = 0; i < 20; i++) {
                const rock = res[randomRangeInt(0, res.length)];
                const ndRock = new Node();
                ndRock.layer = Layers.Enum.UI_2D;
                ndRock.addComponent(UITransform);
                ndRock.addComponent(Sprite).spriteFrame = rock;
                ndRock.parent = this.ndGround;
            }

            this.ndGround.children.forEach(child => {
                child.setPosition(randomRangeInt(-1000, 1000), randomRangeInt(-1000, 1000));
            })
        })
    }



}


